using System;
using System.Collections.Generic;
using System.ComponentModel.Design;
using Game.Config;
using UnityEngine;

public class BagData
{

    static BagData instance = new BagData();
    public static BagData Instance => instance;
    //每一个格子的信息
    public Dictionary<int, BagEntity> dct = new Dictionary<int, BagEntity>();


    public int max = 54;


    List<BagEntity> props;
    List<BagEntity> equips;
    List<BagEntity> mats;
    public void SortByType()
    {
        if(props == null)
        {
            props= new List<BagEntity>();
            equips = new List<BagEntity>();
            mats = new List<BagEntity>();
        }
        
        foreach(var item in dct.Values)
        {
            if(item.propConfig.type ==0)
            {
                props.Add(item);
            }
            else if (item.propConfig.type == 1)
            {
                equips.Add(item);
            }
            else if (item.propConfig.type == 2)
            {
                mats.Add(item);
            }        
        }
        dct.Clear();

        foreach(var item in props)
        {
            Add(item.id,item.num);
        }
        foreach (var item in equips)
        {
            Add(item.id, item.num);
        }

        foreach (var item in mats)
        {
            Add(item.id, item.num);
        }

        BagViewController.Instance.view.Show();
        props.Clear();
        equips.Clear();
        mats.Clear();

    }



    public void Add(int id,int num ,Action<BagEntity> cb =null ,Action<int> Error = null)
    {
        if(dct.Count > max) { Error?.Invoke(0);  return; }
        int grild = -1;

        foreach (var item in dct)
        {
            if (item.Value.propConfig.id == id)
            {
                grild = item.Value.grild_id;
                break;
            }

        }
        if (dct.ContainsKey(grild) && dct[grild].propConfig.superposition == 1)
        {
            dct[grild].num += num;
            cb?.Invoke(dct[grild]);
            Debug.Log($"成功叠加物品 :{id} ，数量为{num}" );
        }
        else if (!dct.ContainsKey(grild) || dct[grild].propConfig.superposition == 0)
        {
            PropEntity propEntity = PropData.Get(id);   
            if(propEntity.superposition == 0)  
            {
                for (int j = 0; j < num; j++)
                {
                    if (dct.Count > max) { Error?.Invoke(0); return; }
                    BagEntity bagEntity = new BagEntity();
                    bagEntity.id = id;
                    bagEntity.num = 1;
                    bagEntity.propConfig = propEntity;


                    //找格子 找到一个空格子 然后丢进去
                    for (int i = 0; i < max; i++) //什么意思？ 意思是想做一个有序 表？
                    {
                        if (dct.ContainsKey(i))
                        {
                            continue; 
                        }
                        else
                        {
                            bagEntity.grild_id = i;
                            dct[i] = bagEntity;
                            cb?.Invoke(bagEntity);
                            break;
                        }
                    }
                    Debug.Log($"成功新增不可叠加物品 :{id} ，数量为{num} ,,格子id为:{j}");
                }
            }
            else if (propEntity.superposition == 1)
            {
                BagEntity bagEntity = new BagEntity();
                bagEntity.id = id;
                bagEntity.num = num;
                bagEntity.propConfig = propEntity;
                for (int i = 0; i < max; i++) //什么意思？ 意思是想做一个有序 表？
                {
                    if (dct.ContainsKey(i))
                    {
                        continue;
                    }
                    else
                    {
                        bagEntity.grild_id = i;
                        dct[i] = bagEntity;
                        cb?.Invoke(bagEntity);
                        return;
                    }
                }

            }

        }
    }

    /// <summary>
    /// 根据在背包中的格子id来删除
    /// </summary>
    /// <param name="grild_id"> 背包中的格子ID </param>
    /// <param name="num"></param>
    /// <param name="cb"></param>
    public void Remove_By_Grild_Id (int grild_id,int num ,Action<BagEntity>cb =null)
    {
        if(dct.ContainsKey(grild_id) == false)
        {
            Debug.Log($"背包中不含有物品: __{grild_id}");
            return;
        }
        var e = dct[grild_id];

        //这里删除的逻辑是不是有问题
        if(e.propConfig.superposition != 0) 
        {
            if (e.num >= num)
            {
                e.num -= num;
                if (e.num == 0)
                {
                    dct.Remove(grild_id);
                }
                cb?.Invoke(e);
            }
            else
            {
                Debug.Log($"背包中物品: __{grild_id} ，，数量 < {num}");
            }
        }
        else
        {
            dct.Remove(grild_id);
            cb?.Invoke(e);
        }

    }

    public void Remove_By_Prop_Id(int id,int num, Action<BagEntity> cb = null)
    {
        var loop_count = PropData.Get(id).superposition ==0 ? 1:num;
        for(int i=0; i<loop_count; i++)
        {
            int _temp_grild_id = 0;
            foreach (var kv in dct)
            {
                if (kv.Value.propConfig.id == id)
                {
                    _temp_grild_id = kv.Value.grild_id;
                    Remove_By_Grild_Id(kv.Key, num, cb);
                }
            }
            if (_temp_grild_id != 0)
            {
                Remove_By_Grild_Id(_temp_grild_id, num, cb);
            }
        }

    }
    public BagEntity Get(int grild_id)
    {
        if(dct.TryGetValue(grild_id,out var e))
        {
            return e;
        }
        return null;
    }

    //根据物品 返回指定数量 
    public int Query(int id)
    {
        int count = 0;
        foreach(var kv in dct)
        {
            if(kv.Value.propConfig.id == id)
            {
                count += kv.Value.num;
            }
        }
        return count;
    } 

    public void Reset()
    {
        dct.Clear();
    }
    /// <summary>
    /// 物体位置的交换 entity1 和entity2 不为空
    /// </summary>
    /// <param name="entity1"></param>
    /// <param name="entity2"></param>
    /// <param name="cb"></param>
    public void Modify_Grild(BagEntity entity1,BagEntity entity2 ,Action<BagEntity, BagEntity> cb =null)
    {
        dct[entity1.grild_id] = entity2;
        dct[entity2.grild_id] = entity1;

        var tempGrild_id = entity2.grild_id;
        entity2.grild_id = entity1.grild_id;
        entity1.grild_id = tempGrild_id;

        cb?.Invoke(entity1, entity1);
    }

    /// <summary>
    /// 物体位置的交换 entity1 和entity2 任意为空的情况
    /// </summary>
    /// <param name="entity1"></param>
    /// <param name="entity2"></param>
    /// <param name="cb"></param>
    public void Modify_Grild(BagEntity entity2,int grild_id_1, Action<BagEntity, BagEntity> cb = null)
    {

        var entity1 = BagData.instance.Get(grild_id_1);
        if(entity1 == null)
        {
            dct.Remove(entity2.grild_id);
            dct[grild_id_1] = entity2;
        }
        
        dct[entity1.grild_id] = entity2;
        dct[entity2.grild_id] = null;

        var tempGrild_id = entity2.grild_id;
        entity2.grild_id = entity1.grild_id;
        entity1.grild_id = tempGrild_id;

        cb?.Invoke(entity1, entity1);
    }

    public void Modify_Grild(int grild_id_1, int grild_id_2, Action<BagEntity, BagEntity> callback)
    {
        var entity1 = Get(grild_id_1);
        var entity2 = Get(grild_id_2);
        if (entity1 == null)
        {
            dct.Remove(grild_id_2);
            if (entity2 == null)
            {
                dct.Remove(grild_id_1);
            }
            else
            {
                dct[grild_id_1] = entity2;
                entity2.grild_id = grild_id_1;
            }
        }
        else
        {
            if (entity2 == null)
            {
                dct.Remove(grild_id_1);
                dct[grild_id_2] = entity1;
                entity1.grild_id = grild_id_2;
            }
            else
            {
                dct[grild_id_1] = entity2;
                dct[grild_id_2] = entity1;
                entity1.grild_id = grild_id_2;
                entity2.grild_id = grild_id_1;
            }
        }
        callback?.Invoke(entity1, entity2);
    }


    public void EquipSwitch(int equip_grild_id, int prop_grild_id, Action<BagEntity, BagEntity> callback = null)
    {
        BagEntity equ_entity;
        if (equip_dct.TryGetValue(equip_grild_id,out equ_entity) == false)
        {
            Debug.LogError($"{equip_grild_id} is null!!");
            return;
        }

        BagEntity prop_entity;

        //1、背包的格子为空  ->卸下 逻辑
        //2、end不是装备 || 部位不一致  ->直接add进去 卸下逻辑 
        //3、end是装备 而且 部位一致 装备 交换 装备逻辑

        dct.TryGetValue(prop_grild_id, out prop_entity);

        if (prop_entity == null)
        {
            //直接 equip 直接卸下 
            Add(equ_entity.propConfig.id, equ_entity.num);
            equip_dct.Remove(equip_grild_id);
        }
        else
        {
            if(equ_entity.propConfig.type  != prop_entity.propConfig.type || equ_entity.propConfig.part != prop_entity.propConfig.part)
            {
                Add(equ_entity.propConfig.id, equ_entity.num);
                equip_dct.Remove(equip_grild_id);
            }
            else if(equ_entity.propConfig.part == prop_entity.propConfig.part)
            {
                //bag -> equ
                equip_dct[equ_entity.grild_id] = prop_entity;
                dct[prop_entity.grild_id] = equ_entity;

                equ_entity.grild_id = prop_entity.grild_id;
                prop_entity.grild_id = prop_entity.propConfig.part;
            }
            callback?.Invoke(equ_entity, prop_entity);


        }
        
    }

    /// <summary>
    /// key: equip_type 
    /// </summary>
    public Dictionary<int,BagEntity>equip_dct = new Dictionary<int,BagEntity>();

    public void Add_Equip(BagEntity bagEntity)
    {
        //if (equip_dct.ContainsKey(bagEntity.propConfig.part))
        //{
        //    //bagEntity.grild_id = bagEntity.propConfig.part;
        //}
        //else
        //{
        //}
        equip_dct[bagEntity.propConfig.part] = bagEntity;
        bagEntity.grild_id = bagEntity.propConfig.part;
    }

    public void Remove_Equip(BagEntity bagEntity)
    {
        if (equip_dct.ContainsKey(bagEntity.propConfig.part))
        {
            equip_dct.Remove(bagEntity.propConfig.part);
        }
    }

    public BagEntity Get_Equip(int part)
    {
        if(equip_dct.TryGetValue(part, out var equip))
        {
            return equip;
        }
        return null;
    }

    //交换部位 部位已装备的数据 ->迁移到背包  删除已装备的数据
    //然后再添加 从背包拿出的装备
    public void Switch_Equip(BagEntity bagItem)
    {
        if(bagItem != null)
        {
            if(bagItem.propConfig.type == 1)
            {
                if (equip_dct.ContainsKey(bagItem.propConfig.part))
                {

                    BagEntity target_equip = equip_dct[bagItem.propConfig.part];
                    //bag -> equ
                    equip_dct[bagItem.propConfig.part] = bagItem;
                    dct[bagItem.grild_id] = target_equip;

                    target_equip.grild_id = bagItem.grild_id;
                    bagItem.grild_id = bagItem.propConfig.part;
                    //Remove_Equip(bagItem);
                    //Add(bagItem.id, 1); //添加到物品栏  
                    //Add_Equip(bagItem);//添加到装备栏 完成交换
                }
                else
                {
                    Remove_By_Grild_Id(bagItem.grild_id,1); //从背包中删除掉
                    Add_Equip(bagItem); //添加到装备栏
                }
            }
        }
    }
}

public class BagEntity
{
    public int id;         //

    public int grild_id;  //在背包中的格子ID
    public int num; //物体数量
    public PropEntity propConfig;
}
